﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;
using System.Collections;
using System.Collections.Generic;

namespace Model
{
    [Serializable()]
    public class SoQuyTienMatBOCollection : Csla.BusinessListBase<SoQuyTienMatBOCollection, SoQuyTienMatBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            SoQuyTienMatBO _SoQuyTienMatBO = SoQuyTienMatBO.NewChildSoQuyTienMatBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_SoQuyTienMatBO));
            Add(_SoQuyTienMatBO);
            return _SoQuyTienMatBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private SoQuyTienMatBOCollection()
        {
        }

        public static SoQuyTienMatBOCollection NewSoQuyTienMatBOCollection()
        {
            return new SoQuyTienMatBOCollection();
        }

        public static SoQuyTienMatBO NewSoQuyTienMatBO()
        {
            return SoQuyTienMatBO.NewChildSoQuyTienMatBO();
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection()
        {
            SPParam spParam = new SPParam("NgayLapPhieu,So", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("NgayLapPhieu,So", "ASC", "");
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where)
        {
            SPParam spParam = new SPParam("NgayLapPhieu,So", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("NgayLapPhieu,So", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "NgayLapPhieu,So" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "NgayLapPhieu,So" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "NgayLapPhieu,So" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }

        public static SoQuyTienMatBOCollection GetSoQuyTienMatBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "NgayLapPhieu,So" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoQuyTienMatBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("THThanhToanVoiNguoiMuaBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                double phatSinhNo = 0;
                double phatSinhCo = 0;
                double tonQuy = 0;

                double thu = 0;
                double chi = 0;

                while (dr.Read())
                {
                    SoQuyTienMatBO objSoQuyTienMatBO = SoQuyTienMatBO.GetChildSoQuyTienMatBO(dr);
                    thu = StaticUtil.GetDouble(objSoQuyTienMatBO.SoTienThu, 0);
                    chi = StaticUtil.GetDouble(objSoQuyTienMatBO.SoTienChi, 0);

                    phatSinhNo += thu;
                    phatSinhCo += chi;
                    tonQuy = tonQuy + thu - chi;

                    objSoQuyTienMatBO.TonQuy = tonQuy;
                    this.Add(objSoQuyTienMatBO);
                }

                if (this.Count > 0)
                {
                    SoQuyTienMatBO objSoQuyTienMatBO_Total = SoQuyTienMatBOCollection.NewSoQuyTienMatBO();
                    objSoQuyTienMatBO_Total.Flag = -1;
                    objSoQuyTienMatBO_Total.STT0 = null;
                    objSoQuyTienMatBO_Total.DienGiai = "TỔNG CỘNG";
                    objSoQuyTienMatBO_Total.SoTienThu = phatSinhNo;
                    objSoQuyTienMatBO_Total.SoTienChi = phatSinhCo;
                    objSoQuyTienMatBO_Total.TonQuy = tonQuy;
                    this.Add(objSoQuyTienMatBO_Total);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public SoQuyTienMatBO GetSoQuyTienMatBO(int _ThuChiID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThuChiID == _ThuChiID)
                    return this[i];

            return null;
        }

        public void Remove(int ThuChiID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].ThuChiID == ThuChiID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
